#include "lexer.h"
#include "error.h"
#include <iostream> 
using namespace std;

Lexer::Token_value Lexer::curr_tok;
double Lexer::number_value;
std::string Lexer::string_value;

Lexer::Token_value Lexer::get_token()	//从输入序列中逐个字符分析，读取一个记号（符号）给curr_tok 
{
	char ch = 0;
	cin.get(ch);       //从输入序列取一个字符
	while (ch != '\n' && isspace(ch)) {   //属于非换行字符的其他空白字符是取下一个字符
		if (!cin.get(ch)) return curr_tok = END;    //输入结束
	}

	switch (ch) {
	case 0:  return END; 
	case ';':
	case '\n': return curr_tok = PRINT;
	case '*':
	case '/':
	case '+':
	case '-':
	case '(':
	case ')': 
	case '=': return curr_tok = Token_value(ch);
	case '0': case '1': case '2':case '3':case '4':
	case '5': case '6': case '7':case '8':case '9':
	case '.': { //NUMBER
		cin.putback(ch);
		cin >> number_value;
		return curr_tok = NUMBER;
	}
	default: {  //NAME(字母开头的字母数字串),NAME=,or error  Page115

		if (isalpha(ch)) {
			string_value = ch;
			while (cin.get(ch) && isalnum(ch))
				string_value.push_back(ch);
			cin.putback(ch);
			return curr_tok = NAME;
		}
		Error::error("bad token");
		return curr_tok = PRINT;
	}
	}
}

void Lexer::output_token(){
    cout<<"Token:";
    switch (curr_tok)
    {
    case NUMBER:
        cout<< number_value <<endl;
        break;
    case NAME:
        cout<<string_value <<endl;
        break;
    default:
        cout<<char(curr_tok)<<endl;
    }
}